<?php
class ClientUser extends CI_Controller{
	
	public function __construct(){
		parent::__construct();
		
		//check loggin
		if ($this->session->userdata('LoggedIn')){
			//load defense model
			$this->load->model('UserModel', 'user', TRUE);
		}else{
			$arr = array('status' => 'fail', 'message' => 'error message here ccc');
			echo json_encode($arr);
			exit();
		}
	}
	
	public function all_user_detail(){
		$users = $this->user->all_user();
		
		if ($users == false) {
			$arr = array('status' => 'fail', 'message' => 'error message here');
			echo json_encode($arr);
			exit();
		}else {
			foreach ($users as $user){
				if ($user->manager_id == null){
					$user->manager_id = 0;
				}
			}
			$arr = array('status' => 'success', 'info' => $users);
			$json = json_encode($arr);
			print_r($json);
		}
	}
	
	public function user_detail(){
		if (!isset($_GET['id']) || empty($_GET['id'])){
			$arr = array('status' => 'fail', 'message' => 'error message here');
			echo json_encode($arr);
			exit();
		}
		
		$user_id = $_GET['id'];
		$user = $this->user->detail_user($user_id);
		
		if ($user == false){
			$arr = array('status' => 'fail', 'message' => 'error message here');
			echo json_encode($arr);
			exit();
		}else {
			unset($user->password);
			$arr = array('status' => 'success', 'info' => $user);
			$json = json_encode($arr);
			print_r($json);
		}
	}
	
	public function delete_user(){
		if (!isset($_GET['id']) || empty($_GET['id'])){
			$arr = array('status' => 'fail', 'message' => 'error message here');
			echo json_encode($arr);
			exit();
		}
		
		$user_id = $_GET['id'];
		
		$this->db->trans_start();
		
		$this->user->delete_user($user_id);
		
		if ($this->db->trans_status() === FALSE){
			$this->db->trans_rollback();
			$arr = array('status' => 'fail', 'message' => 'error message here');
			echo json_encode($arr);
			return;
		}else{
			$this->db->trans_commit();
			$arr = array('status' => 'success');
			echo json_encode($arr);
			return;
		}
	}
	
	public function edit_user(){
		//get id of user
		if (!isset($_GET['id']) || empty($_GET['id'])){
			$arr = array('status' => 'fail', 'message' => 'error message here 1');
			echo json_encode($arr);
			return;
		}
		
		$user_id = $_GET['id'];
		
		//get first name of user
		if (!isset($_GET['first_name']) || empty($_GET['first_name'])){
			$arr = array('status' => 'fail', 'message' => 'error message here 2');
			echo json_encode($arr);
			return;
		}
		
		$userDetail['first_name'] = $_GET['first_name'];
		
		//get last name of user
		if (!isset($_GET['last_name']) || empty($_GET['last_name'])) {
			$arr = array('status' => 'fail', 'message' => 'error message here 3');
			echo json_encode($arr);
			return;
		}
		
		$userDetail['last_name'] = $_GET['last_name'];
		
		//get birthday of user
		if (!isset($_GET['date_of_birth'])) {
			$arr = array('status' => 'fail', 'message' => 'error message here 4');
			echo json_encode($arr);
			return;
		}
		
		$userDetail['date_of_birth'] = $_GET['date_of_birth'];
		
		//get address of user
		if (!isset($_GET['address'])){
			$arr = array('status' => 'fail', 'message' => 'error message here 5 ');
			echo json_encode($arr);
			return;
		}
		
		$userDetail['address'] = $_GET['address'];
		
		//get phone_number of user
		if (!isset($_GET['phone_number'])) {
			$arr = array('status' => 'fail', 'message' => 'error message here 6 ');
			echo json_encode($arr);
			return;
		}
		
		$userDetail['phone_number'] = $_GET['phone_number'];
		
		//update user:
		$affected = $this->user->update_user($user_id, $userDetail);
		if ($affected > 0){
			$info = $this->user->detail_user($user_id);
			$arr = array('status' => 'success', 'info' => $info);
			echo json_encode($arr);
		}else{
			$arr = array('status' => 'fail', 'message' => 'error message here 8 ');
			echo json_encode($arr);
			return;
		}
	}
}